﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Ext.Net;

using VTV.TaskManagementSystem.Common.Constant;

namespace VTV.TaskManagementSystem.WebApp.Admin.Role
{
    public partial class List : System.Web.UI.Page
    {
        private static bool _isUpdate;
        private static int _RoleID;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["UserID"] == null || Session["UserName"] == null || Session["RoleLevel"] == null)
            {
                Response.Redirect("~/Admin/Login.aspx?return=Admin/Role/List.aspx");
            }
        }

        protected void btnAdd_Click(object sender, DirectEventArgs e)
        {
            _isUpdate = false;
            this.windowDetailRole.Icon = Icon.Add;
            this.windowDetailRole.Title = "Thêm mới";
            this.windowDetailRole.Show();
        }

        protected void btnSave_Click(object sender, DirectEventArgs e)
        {
            var obj = new BusinessLogic.Role();
            var entityData = new Entities.Role
            {
                RoleID = !String.IsNullOrEmpty(this.RoleID.Text.Trim()) ? Convert.ToInt32(this.RoleID.Text) : 0,
                Name = String.IsNullOrEmpty(txtName.Text.Trim()) ? string.Empty : txtName.Text.Trim(),
                Descriptions = String.IsNullOrEmpty(txtNote.Text.Trim()) ? string.Empty : txtNote.Text.Trim()
            };

            if (_isUpdate)
            {
                var result = obj.Update(entityData);

                if (result)
                {
                    this.storeEmployee.Reload();

                    X.Msg.Notify(SystemInfoMessage.SYS_TITLE_VN, AuditInfoMessage.UPDATE_SUCCESS_DETAIL_VN.Replace("{0}", txtName.Text)).Show();
                }
                else
                {
                    X.Msg.Notify(SystemInfoMessage.SYS_TITLE_VN, AuditInfoMessage.AUDIT_FAILSE_VN.Replace("{0}", obj.ErrorMessage)).Show();
                }
            }
            else
            {
                var result = obj.AddNew(entityData);

                if (result)
                {
                    this.storeEmployee.Reload();

                    X.Msg.Notify(SystemInfoMessage.SYS_TITLE_VN, AuditInfoMessage.INSERT_SUCCESS_DETAIL_VN.Replace("{0}", txtName.Text)).Show();
                }
                else
                {
                    X.Msg.Notify(SystemInfoMessage.SYS_TITLE_VN, AuditInfoMessage.AUDIT_FAILSE_VN.Replace("{0}", obj.ErrorMessage)).Show();
                }
            }
        }

        protected void gridRole_OnCommand(object sender, DirectEventArgs e)
        {
            _RoleID = Convert.ToInt32(e.ExtraParams["RoleID"]);
            var command = e.ExtraParams["Command"];

            if (command.Equals("Edit"))
            {
                _isUpdate = true;
                var obj = new BusinessLogic.Role();
                var entity = obj.GetRole(_RoleID);

                if (entity != null && string.IsNullOrEmpty(obj.ErrorMessage))
                {
                    if (!this.SetValue(entity))
                    {
                        this.btnSave.Enable(false);
                        X.Msg.Notify(AuditInfoMessage.AUDIT_TITLE_VN, AuditInfoMessage.TRANSFER_FAILSE_VN + "<BR>" + AuditHelpMessage.PLS_BACK_LASTER).Show();
                    }
                }

                this.windowDetailRole.Icon = Icon.ReportEdit;
                this.windowDetailRole.Title = "Cập nhật thông tin";
                this.windowDetailRole.Show();
            }
            else
            {
                X.Msg.Confirm(AuditInfoMessage.AUDIT_TITLE_VN, AuditInfoMessage.DELETE_CONFIRM_VN,
                              new MessageBoxButtonsConfig()
                              {
                                  Yes = new MessageBoxButtonConfig
                                  {
                                      Text = "Xác nhận",
                                      Handler = "Role.List.DeleteEmployee()"
                                  },
                                  No = new MessageBoxButtonConfig
                                  {
                                      Text = "Quay lại"
                                  }

                              }).Show();
            }

        }

        protected void storeEmployee_OnReadData(object sender, StoreReadDataEventArgs e)
        {
            var start = e.Start;
            var limit = e.Limit;

            var source = new BusinessLogic.Role().GetAll(start, limit);
            var total = new BusinessLogic.Role().GetCount();

            if (source != null && source.Any() && total != null)
            {
                var data = new Paging<Entities.Role>(source, (int)total);

                e.Total = data.TotalRecords;

                this.storeEmployee.DataSource = data.Data;
                this.storeEmployee.DataBind();
            }
        }

        [DirectMethod(Namespace = "Role.List")]
        public void DeleteEmployee()
        {
            var obj = new BusinessLogic.Role();
            var entity = obj.GetRole(_RoleID);
            if (entity != null && string.IsNullOrEmpty(obj.ErrorMessage))
            {
                var result = obj.Delete(entity);

                if (result)
                {
                    this.storeEmployee.Reload();

                    X.Msg.Notify(SystemInfoMessage.SYS_TITLE_VN, AuditInfoMessage.DELETE_SUCCESS_VN).Show();
                }
                else
                {
                    X.Msg.Notify(SystemInfoMessage.SYS_TITLE_VN, AuditInfoMessage.AUDIT_FAILSE_VN.Replace("{0}", obj.ErrorMessage)).Show();
                }
            }
            //X.Msg.Notify("delete", _RoleID.ToString()).Show();
        }

        private bool SetValue(Entities.Role entity)
        {
            try
            {
                txtName.Text = entity.Name;
                txtNote.Text = entity.Descriptions;
                RoleID.Text = entity.RoleID.ToString();

                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }
    }
}